<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>
        家庭成员添加
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="css/main.css" media="all">
    <script type="text/javascript" src="lib/loading/okLoading.js"></script>
    <style>
        .imgs{display: none;}
    </style>

</head>
<body>

<div class="x-body">
    <form class="layui-form layui-form-pane" action="${pageContext.request.contextPath}/FamilyServlet" method="post" id="add-form" enctype="multipart/form-data">
        <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
            <div class="layui-tab-content" >
                <div class="layui-tab-item layui-show">
                    <input type="hidden" name="op" value="add">
                    

                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>姓名
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="familyName" autocomplete="off" placeholder=" "
                                   class="layui-input" value="" required>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>身份证号码
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="familyCard" autocomplete="off" placeholder=" "
                                   class="layui-input" value="" required>
                        </div>
                    </div>
                    
                    
                    
                   
                    
                    
                    
                    <div class="layui-form-item">
                        <label  class="layui-form-label">
                            <span class="x-red" >*</span>相片
                        </label>
                        <div class="layui-input-block">
                           <div class="site-demo-upbar">
                                <div class=" layui-upload-button" style="border:#FFFFFF ;">
                                    <button type="button" class="layui-btn" id="test1">
                                        <i class="layui-icon">&#xe67c;</i>上传图片
                                    </button>
                                    <input class="layui-upload" type="file"  id="previewImg" name="familyImage"  required>
                                </div>

                            </div>
                        </div>
                       </div> 
                       
                       <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>相片预览
                        </label>
                        <div class="layui-input-block">
                            <img src="" id="image-show" name="pimages" style="width: 400px;height: 200px;"/>
                        </div>
                    </div>
                       
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>门牌号
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="roomNo" autocomplete="off" placeholder=" "
                                   class="layui-input" value=""  required>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>类型（与户主关系）
                        </label>
                        <div class="layui-input-block">
                            <select name="familyTypeId" id="typeId">

                                <option value="0">---请选择类型---</option>
                                
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>手机号码
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="familyPhone" autocomplete="off" placeholder=" "
                                   class="layui-input" value=""  required>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <button class="layui-btn" type="submit" lay-filter="add" lay-submit="">
                            保存
                        </button>
                    </div>

                    <div style="height:100px;"></div>
                </div>



            </div>

        </div>
    </form>
</div>
<script src="lib/layui/layui.js" charset="utf-8"></script>
<script src="js/x-layui.js" charset="utf-8"></script>
 
<script type="text/javascript" src="js/jquery.js"></script>
<script src="js/jquery-form.js"></script>
<script>
//获取商品类型列表
$.getJSON("${pageContext.request.contextPath}/FamilyTypeServlet",{op:"getList"},function(result,status,xhr){
	let content = "";
	$.each(result.data,function(i,obj){
		content+=
			
			"<option value=\""+obj.familyTypeNo+"\">"+obj.familyTypeName+"</option>";
		 
	});
	//填充到下拉框
	$("#typeId").html(content);
});

//对指定的表单绑定提交事件
$("#add-form").submit(function(){
	$(this).ajaxSubmit(function(result){
		//使用layui弹出提示信息
		layui.use('layer',function(){
			layer.msg(result.message,{icon:1,time:3000});
			if(result.status!=0){
				window.setTimeout(function(){
					//调用首页的查询
					window.parent.family_search();
					//关闭子窗口
					var index = parent.layer.getFrameIndex(window.name);
					parent.layer.close(index);
				},3000);
			}
		});
		
		
	});
	//限制表单的默认提交
	return false;
});

//图片预览 为文件上传绑定change事件
$("#previewImg").change(function(event){
	//根据这个input获取文件的 HTML5 js 对象
    let files = event.target.files, file;
    if (files && files.length > 0) {
        // 获取目前上传的图片
        file = files[0];
        // 控制打印file对象
        console.log(file);
        // 限制图片上传大小
        if (file.size > 102400) {
            alert('图片大小不能超过 100kb!');
            $("#input-image").val("");
            return false;
        }
        // 通过这个 file 对象生成一个可用的图像 URL
        // 获取 window 的 URL 工具
        let URL = window.URL || window.webkitURL;
        // 通过 file 生成目标 url
        let imgURL = URL.createObjectURL(file);
        $("#image-show").attr("src", imgURL);
    }
	
});
</script>



<script>
    layui.use(['laydate','element','layer','form'], function(){
        $ = layui.jquery;//jquery
        lement = layui.element();//面包导航
        layer = layui.layer;//弹出层
        form = layui.form();
        laydate = layui.laydate;//日期插件
        okLoading.close($);
        form.render();
        var start = {
            min: laydate.now()
            ,max: '2099-06-16 23:59:59'
            ,istoday: false
            ,choose: function(datas){
                //end.min = datas; //开始日选好后，重置结束日的最小日期
                //end.start = datas //将结束日的初始值设定为开始日
            }
        };
        document.getElementById('LAY_demorange_s').onclick = function(){
            start.elem = this;
            laydate(start);
        };


        //图片上传接口
        /*layui.upload({
          url: './upload.json' //上传接口
          ,success: function(res){ //上传成功后的回调
              console.log(res);
            $('#LAY_demo_upload').attr('src',res.url);
          }
        });*/


        //监听提交
        form.on('submit(add)', function(data){
            var device_name=$("input[name='device_name']").val();
            var brand=$("input[name='brand']").val();
            var money=$("input[name='money']").val();
            var number=$("input[name='number']").val();
            var sy_year=$("input[name='sy_year']").val();
            var gm_time=$("input[name='gm_time']:checked").val();
            var community_id=$('#community_id option:selected') .val();//所属栏目ID

            if(device_name==""){
                layer.msg('设备名称不能为空',{icon:5,time:2000});return false;
            }
            if(brand==""){
                layer.msg('品牌名称不能为空',{icon:5,time:2000});return false;
            }
            if(money==""){
                layer.msg('购买单价不能为空',{icon:5,time:2000});return false;
            }
            if(community_id==""){
                layer.msg('所属小区不能为空',{icon:5,time:2000});return false;
            }
            if(number==""){
                layer.msg('购买数量不能为空',{icon:5,time:2000});return false;
            }
            if(sy_year==""){
                layer.msg('预计使用年限不能为空',{icon:5,time:2000});return false;
            }
            if(gm_time==""){
                layer.msg('购买日期不能为空',{icon:5,time:2000});return false;
            }

            var data=data.field;
            $.ajax({
                type:"post",
                url:"xxx",
                data:data,
                dataType:"json",
                success:function(data){
                    if(data.status==1){
                        layer.msg(data.info, {icon: 6,time:2000},function () {
                            window.parent.location.reload();
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        });
                        return false;

                    }
                    else{
                        layer.msg(data.info,{icon:5,time:2000});return false;
                    }
                }

            });

        });

    })
</script>
<!--栏目缩略图上传-->
<script>
    function upload()
    {
        var formData = new FormData();
        formData.append('images', $('#previewImg')[0].files[0]);
        //console.log(formData);
        layer.msg('图片上传中', {icon: 16});
        $.ajax({
            type:"post",
            processData: false,
            contentType: false,
            url:"xxx",
            data:formData,
            success:function(data){
                if(data.status == 1){
                    console.log(data.image_name);
                    layer.closeAll('loading');
                    //layer.msg(data.info,{icon:1,time:1000});
                    $("#pimages").attr('src',data.image_name);
                    $("#avatar").val(data.image_name);
                    $(".imgs").show();
                    return false;
                }else{
                    layer.msg(data.info,{icon:2,time:1000});
                }
            }
        });
    }
</script>

</body>
</html>